package com.dw.love.dwade.utils;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import static android.support.constraint.Constraints.TAG;

/**
 * Created by luozizhao on 2018/6/15.
 */

public class BitmapUtils {

    private static final String TAG = "BitmapUtils";

    //根据期望的宽高，压缩图片。
    //例如imgview设定为200*200  源图片300*500 则会根据 （300/200）= inSamPleSize = 2 进行压缩（2隔两个像素点取点）。
    public static int calculateInSampleSize(BitmapFactory.Options options, int targetWidth, int targetHeight) {
        int inSampleSize = 1;
        int height = options.outHeight;
        int width = options.outWidth;
        if (height > targetHeight || width > targetWidth) {
            int heightRatio = (int) ((float) height / (float) targetHeight);
            int wightRatio = (int) ((float) width / (float) targetWidth);
            inSampleSize = heightRatio > wightRatio ? wightRatio : heightRatio;
        }
        return inSampleSize;
    }

    public static int calculateInSampleSize(Bitmap bitmap, int targetWidth, int targetHeight) {
        int inSampleSize = 1;
        int height = bitmap.getHeight();
        int width = bitmap.getWidth();
        if (height > targetHeight || width > targetWidth) {
            int heightRatio = (int) ((float) height / (float) targetHeight);
            int wightRatio = (int) ((float) width / (float) targetWidth);
            inSampleSize = heightRatio > wightRatio ? wightRatio : heightRatio;
        }
        return inSampleSize;
    }

    public static Bitmap ChangeBitmapInSampleSize(int inSampleSize, Bitmap bitmap) {


        /*ByteArrayOutputStream out = new ByteArrayOutputStream();
　　image.compress(Bitmap.CompressFormat.JPEG, 100, out);
　　BitmapFactory.Options newOpts = new BitmapFactory.Options();
　　int be = 2;
　　newOpts.inSampleSize = be;
　　ByteArrayInputStream isBm = new ByteArrayInputStream(out.toByteArray());
　　Bitmap bitmap = BitmapFactory.decodeStream(isBm, null , null );*/

        BitmapFactory.Options opts = new BitmapFactory.Options();
        ByteArrayOutputStream dataByte = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, dataByte);
        Log.d(TAG, "ChangeBitmapInSampleSize: 设置采样率前："+bitmap.getByteCount()+",inSampleSize:"+opts.inSampleSize);
        opts.inSampleSize = inSampleSize;
        opts.inPreferredConfig = Bitmap.Config.RGB_565;
        ByteArrayInputStream result = new ByteArrayInputStream(dataByte.toByteArray());
        Bitmap bitmap1 = BitmapFactory.decodeStream(result);
        Log.d(TAG, "ChangeBitmapInSampleSize: 设置采样率后："+bitmap1.getByteCount()+",inSampleSize:"+opts.inSampleSize);
        return bitmap1;
    }


    //res to bitmap
    public static Bitmap decodeSampleSizeFormResoure(Resources resources, int resId, int targetWidth, int targetHeight) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        //1.inJustDecodeBounds设true，拿到原图参数
        options.inJustDecodeBounds = true;// 计算过程bitmap返回为null（这里主要为了拿到原图宽高参数）
        BitmapFactory.decodeResource(resources, resId, options);


        //2.计算压缩比
        options.inSampleSize = calculateInSampleSize(options, targetWidth, targetHeight);//压缩

        //3.设置图片参数
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
//        Bitmap bitmapResult = BitmapFactory.decodeResource(resources, resId, options);
        InputStream inputStream = resources.openRawResource(resId);
        Bitmap bitmapResult = BitmapFactory.decodeStream(inputStream, null, options);
        Log.d("bitmap", "decodeSampleSizeFormResoure: " + resId + "-->压缩后：" + bitmapResult.getByteCount() / 1024);
        return bitmapResult;
    }

}
